package com.zyw.service.financial.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.mapper.financial.FinancialManagementMapper;
import com.zyw.service.financial.FinancialManagementService;
import com.zyw.util.DateTimeUtil;
import com.zyw.vo.FinancialManagementVo;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class FinancialManagementServiceImpl implements FinancialManagementService {

    @Resource
    public FinancialManagementMapper financialManagementMapper;

    @Override
    public LayUITableVO<List<FinancialManagementVo>> findPage(Map<String, Object> map) {

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));


        List<FinancialManagementVo> list = financialManagementMapper.findPage(map);
        for (FinancialManagementVo financialManagementVo : list) {

            if (financialManagementVo.getOrderStatus()==0){
                financialManagementVo.setOrderStatusString("订单取消");
            }else if (financialManagementVo.getOrderStatus() == 1){
                financialManagementVo.setOrderStatusString("未开始");
            } else if (financialManagementVo.getOrderStatus() == 2){
                financialManagementVo.setOrderStatusString("准备中");
            }else if (financialManagementVo.getOrderStatus() == 3){
                financialManagementVo.setOrderStatusString("已开始");
            }else if (financialManagementVo.getOrderStatus() == 4){
                financialManagementVo.setOrderStatusString("已完成");
            }
            if (financialManagementVo.getOrderStatus()>=1) {
                financialManagementVo.setOrderIncome(String.valueOf(financialManagementVo.getOrderNum() * Double.valueOf(financialManagementVo.getUnitPrice())));
            }
            if (financialManagementVo.getOrderStatus()>=3){


                Date end = null;

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd");

                Date begin = null;
                try {
                    begin = simpleDateFormat.parse(financialManagementVo.getProductionStartDate());
                    end = simpleDateFormat.parse(financialManagementVo.getProductionEndDate());
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                int diffDays = DateTimeUtil.getDiffDays(begin, end);
                financialManagementVo.setStaffExpend(String.valueOf(diffDays*financialManagementVo.getStaffNum()*100));
            }
            if (financialManagementVo.getOrderStatus()==4){
                Double orderIncome = Double.valueOf(financialManagementVo.getOrderIncome());
                Double purchaseExpend = Double.valueOf(financialManagementVo.getPurchaseExpend());
                Double staffExpend = Double.valueOf(financialManagementVo.getStaffExpend());
                financialManagementVo.setProfit(String.valueOf(orderIncome-purchaseExpend-staffExpend));
            }

        }

        PageInfo<FinancialManagementVo> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<FinancialManagementVo>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;

    }
}
